package com.cronlygames.cocos2d.template;

import org.cocos2d.events.CCTouchDispatcher;
import org.cocos2d.layers.CCColorLayer;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccColor3B;
import org.cocos2d.types.ccColor4B;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity {
	private CCGLSurfaceView mGLSurfaceView;
	static Bitmap background = null ;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // set the window status, no tile, full screen and don't sleep
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        mGLSurfaceView = new CCGLSurfaceView(this);
        
        setContentView(mGLSurfaceView);
    
        
        background = BitmapFactory.decodeResource(getResources(), R.drawable.back);
        
        
        // attach the OpenGL view to a window
        CCDirector.sharedDirector().attachInView(mGLSurfaceView);

        // no effect here because device orientation is controlled by manifest
        CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
        
        // show FPS
        // set false to disable FPS display, but don't delete fps_images.png!!
        CCDirector.sharedDirector().setDisplayFPS(false);

        // frames per second
        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);

        CCScene scene = TemplateLayer.scene();

        // Make the Scene active
        CCDirector.sharedDirector().runWithScene(scene);
    }
    
    @Override
    public void onStart() {
        super.onStart();        
    }

    @Override
    public void onPause() {
        super.onPause();

        CCDirector.sharedDirector().pause();
    }

    @Override
    public void onResume() {
        super.onResume();

        CCDirector.sharedDirector().resume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        CCDirector.sharedDirector().end();
    }
    
    
    static class TemplateLayer extends CCColorLayer {
        CCLabel lbl;
        
    	public static CCScene scene() {
    		CCScene scene = CCScene.node();
    		CCLayer layer = new TemplateLayer();
    		
    		scene.addChild(layer);
    		
    		return scene;
    	}

        protected TemplateLayer() {
        	
        	super(new ccColor4B(255, 255, 255, 255));
        	
        	this.setIsTouchEnabled(true);
        	
            CCSprite backSprite = CCSprite.sprite(GameActivity.background, "background");
            CGSize winSize = CCDirector.sharedDirector().winSize();
            
            backSprite.setPosition(winSize.width/2, winSize.height/2);
            
            backSprite.setScaleX(winSize.width/backSprite.getContentSize().width);
            backSprite.setScaleY(winSize.height/backSprite.getContentSize().height);
            
            addChild(backSprite);

        }

        @Override
        public boolean ccTouchesBegan(MotionEvent event) {
        	CGPoint convertedLocation = CCDirector.sharedDirector()
                	.convertToGL(CGPoint.make(event.getX(), event.getY()));
        	
            return CCTouchDispatcher.kEventHandled;
        }

    }

}